data_dir='E:\RawData\';
net_dir='N:\RawData\georg\';
file_error_threshold=1e4;
main_dirs=dir(data_dir);
check_dirs={'Data' 'ImagingData'};
damaged_files={};

for ind=3:length(main_dirs)
    if main_dirs(ind).isdir && (strcmp(main_dirs(ind).name(1:3),'GK_') || strcmp(main_dirs(ind).name(1:3),'AkS'))
        disp('************************************************')
        disp(['Now checking ' main_dirs(ind).name]);
        disp('************************************************')
        for knd=1:length(check_dirs)
            if isdir([data_dir main_dirs(ind).name '\' check_dirs{knd}])
                curr_dirs=dir([data_dir main_dirs(ind).name '\' check_dirs{knd}]);
                for lnd=3:length(curr_dirs)
                    if curr_dirs(lnd).isdir
                        curr_base=[data_dir main_dirs(ind).name '\' check_dirs{knd} '\' curr_dirs(lnd).name];
                        curr_files=dir(curr_base);
                        for mnd=3:length(curr_files)
                            data=[];
                            data2=[];
                            fname=[curr_base '\' curr_files(mnd).name];
                            if 1%rand<0.01
                                disp(['Now checking file ' curr_files(mnd).name]);
                                if strcmp(curr_files(mnd).name(end-3:end),'.lvd')
                                    data=load_lvd(fname);
                                elseif strcmp(curr_files(mnd).name(end-3:end),'.bin')
                                    data=load_bin(fname);
                                elseif strcmp(curr_files(mnd).name(end-3:end),'.eye')
                                    [data, data2]=load_eye_monitor_data(fname, 1);
                                elseif strcmp(curr_files(mnd).name(end-3:end),'.tif')
                                    data=load_tiff(fname);
                                elseif strcmp(curr_files(mnd).name(end-3:end),'.aud')
                                    data=load_aud(fname)/10;
                                end
                                if sum(abs(data(:))>file_error_threshold) || sum(find(abs(diff(data2'))>file_error_threshold))
                                    disp(['WARNING - file ' fname ' potentially damaged']);
                                    ndata=[];
                                    ndata2=[];
                                    nfname=[net_dir main_dirs(ind).name '\' check_dirs{knd} '\' curr_dirs(lnd).name '\' curr_files(mnd).name];
                                    disp(['Now loading net file to compare']);
                                    if strcmp(curr_files(mnd).name(end-3:end),'.lvd')
                                        ndata=load_lvd(nfname);
                                    elseif strcmp(curr_files(mnd).name(end-3:end),'.bin')
                                        ndata=load_bin(nfname);
                                    elseif strcmp(curr_files(mnd).name(end-3:end),'.eye')
                                        [ndata, ndata2]=load_eye_monitor_data(nfname, 1);
                                    elseif strcmp(curr_files(mnd).name(end-3:end),'.tif')
                                        ndata=load_tiff(nfname);
                                    elseif strcmp(curr_files(mnd).name(end-3:end),'.aud')
                                        ndata=load_aud(nfname)/10;
                                    end
                                    damaged_files{end+1,1}=fname;
                                    if abs(sum(ndata(:)-data(:)))>0 || abs(sum(ndata2(:)-data2(:)))>0
                                        damaged_files{end,2}=1;
                                        disp('DAMAGED')
                                        
                                    else
                                        damaged_files{end,2}=0;
                                    end
                                    save damaged_files6 damaged_files
                                end
                            end
                        end
                    end
                end
            end
        end
    end
end
